<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路口编号：" prop="number" :rules="rules.number">
							<el-input :disabled="true" size="default"  placeholder="路口编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路口名称：" prop="name" :rules="rules.name">
							<el-input size="default" placeholder="路口名称" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路口类型：" prop="type" :rules="rules.type">
							<el-select clearable v-model="ruleForm.type"  placeholder="路口类型" style="width: 400px;">
								<el-option label="十字路口" :value="1"></el-option>
								<el-option label="T字路口" :value="2"></el-option>
								<el-option label="环形交叉口" :value="3"></el-option>
								<el-option label="立交桥" :value="4"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="所属区域：" prop="region_id" :rules="rules.region_id">
							<el-select filterable clearable v-model="ruleForm.region_id"  placeholder="所属区域" style="width: 400px;">
								<el-option v-for="item in selectData.regionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item filterable clearable label="所属道路：" prop="road_id" :rules="rules.road_id">
							<el-select  v-model="ruleForm.road_id" placeholder="所属道路" style="width: 400px;">
								<el-option v-for="item in selectData.roadData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="启用状态：" prop="status" :rules="rules.status">
							<el-select   clearable placeholder="启用状态" style="width: 400px;" v-model="ruleForm.status">
								<el-option label="启用" :value="1"></el-option>
								<el-option label="停用" :value="0"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref ,onMounted,watch,defineProps} from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {useBasicApi} from "/@/api/system/menu/index"
import {userApi} from "/@/api/system/user/index"
import {useRoleApi} from "/@/api/system/role/index"
const RoleApi = useRoleApi();
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const BasicApi = useBasicApi()
const useApi = userApi()
const props = defineProps(['tableData','disabled'])
const disabledFlag = ref<Boolean>(false)
// dx定义start
interface RuleForm {
	    number:string,
		name:string,
		status:any,
		region_id:any,
		type:any,
		road_id:any
	}
	const ruleForm = ref<RuleForm>({
		number:"",
		name:"",
		status:null,
		region_id:null,
		type:null,
		road_id:null,
	})

	const searchForm = ref({})
	const selectData = reactive({
		regionData:[],
		roadData:[]
	})

	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '道路编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '道路名称必填', trigger: 'blur' },
		],
		status:[
			{ required: true, message: '启用状态必填', trigger: 'blur' },
		],
		region_id:[
			{ required: true, message: '所属区域必填', trigger: 'blur' },
		],
		type:[
			{ required: true, message: '路口类型', trigger: 'blur' },
		],
		road_id:[
			{ required: true, message: '所属道路必填', trigger: 'blur' },
		]
	})



// dx定义end
	const getSelectData = (async ()=>{
		searchForm.value.search = `status:1`
		let res = await BasicApi.BasicTableApi(searchForm.value);
		if(res.isSuccess){
			selectData.regionData = res.returnData
		}
		let roadRes = await RoleApi.RoleTableApi(searchForm.value);
		if(roadRes.isSuccess){
			selectData.roadData = roadRes.returnData
		}
	})

	onMounted(()=>{
		getSelectData()
	})


	watch(props,(newVal,oldVal)=>{
		ruleForm.value = newVal.tableData
		disabledFlag.value = newVal.disabled
	})







// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		userName: '', // 账户名称
		userNickname: '', // 用户昵称
		roleSign: '', // 关联角色
		department: [] as string[], // 部门
		phone: '', // 手机号
		email: '', // 邮箱
		sex: '', // 性别
		password: '', // 账户密码
		overdueTime: '', // 账户过期
		status: true, // 用户状态
		describe: '', // 用户描述
	},
	deptData: [] as DeptTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
		state.ruleForm = row;
		state.dialog.title = '修改路口';
		state.dialog.submitTxt = '修 改';
	
	state.dialog.isShowDialog = true;
};
const menuDialogFormRef = ref(null)
// 关闭弹窗
const closeDialog = () => {
	// menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async() => {
	
		const res = await useApi.userEditApi(ruleForm.value)
		//console.log(res)
		if(res.isSuccess){
			ElMessage.success('修改成功')
			closeDialog();
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}
	
	// if (state.dialog.type === 'add') { }
});



// 暴露变量
defineExpose({
	openDialog,
});
</script>
